﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using DevComponents.DotNetBar;

namespace PlanningGIS.Symbol
{
    /// <summary>
    /// 作用：栅格渲染
    /// </summary>
    public partial class FrmRasterRender : Office2007Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmRasterRender()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 应用渲染样式事件
        /// </summary>
        public event EventHandler ApplyChangedEvent;

        /// <summary>
        /// 当前作用的栅格图层
        /// </summary>
        public IRasterLayer RasterLayer { get; set; }
        
        /// <summary>
        /// 加载窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmRasterRender_Load(object sender, EventArgs e)
        {
            //this.rasterStretched1.RasterLayer = m_pRasterLayer;
            //this.rasterRGB1.RasterLayer = m_pRasterLayer;
            //this.rasterClassify1.RasterLayer = m_pRasterLayer;
            TreeNode pClassifyNode = new TreeNode("分级");
            TreeNode pStretchNode = new TreeNode("拉伸");
            TreeNode pRgbNode = new TreeNode("RGB组合");
            if (RasterLayer.BandCount == 1)
            {
                this.treeView.Nodes.Add(pStretchNode);
                this.treeView.Nodes.Add(pClassifyNode);
            }
            else if (RasterLayer.BandCount >= 3)
            {
                this.treeView.Nodes.Add(pStretchNode);
                this.treeView.Nodes.Add(pRgbNode);
            }
            this.treeView.ExpandAll();
            this.treeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView_AfterSelect);
            Console.WriteLine(RasterLayer.Renderer.GetType().ToString());
            if (RasterLayer.Renderer is IRasterClassifyColorRampRenderer)
            { 
                this.treeView.SelectedNode = pClassifyNode;
            }
            else if (RasterLayer.Renderer is IRasterRGBRenderer)
            {
                this.treeView.SelectedNode = pRgbNode;
            }
            //else if (m_pRasterLayer.Renderer is IRasterStretchColorRampRenderer)
            //{
            //    this.treeView.SelectedNode = pStretchNode;
            //}
            else
            {
                this.treeView.SelectedNode = pStretchNode;
            }
        }

        /// <summary>
        /// 选择渲染方式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (this.treeView.SelectedNode == null) return;
            switch (this.treeView.SelectedNode.Text)
            {
                case "分级":
                    if (rasterClassify1.RasterLayer == null)
                        rasterClassify1.RasterLayer = RasterLayer;
                    tbCtlRender.SelectedTab = tbItmClassify;
                    break;
                case "拉伸":
                    if (rasterStretched1.RasterLayer == null)
                        rasterStretched1.RasterLayer = RasterLayer;
                    tbCtlRender.SelectedTab = tbItmStretched;
                    break;                    
                case "RGB组合":
                    if (rasterRGB1.RasterLayer == null)
                        rasterRGB1.RasterLayer = RasterLayer;
                    tbCtlRender.SelectedTab = tbItmRGB;
                    break;
            }
        }
        
        /// <summary>
        /// 应用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnApply_Click(object sender, EventArgs e)
        {
            if (this.treeView.SelectedNode == null) return;
            switch (this.treeView.SelectedNode.Text)
            {
                case "分级":
                    this.rasterClassify1.SetRenderer();
                    break;
                case "拉伸":
                    this.rasterStretched1.SetStretchRenderer();
                    break;
                case "RGB组合":
                    this.rasterRGB1.SetRGBRenderer();
                    break;
            }
            if (this.ApplyChangedEvent != null)
            {
                this.ApplyChangedEvent(this, new EventArgs());
            }
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            switch (this.treeView.SelectedNode.Text)
            {
                case "分级":
                    this.rasterClassify1.SetRenderer();
                    break;
                case "拉伸":
                    this.rasterStretched1.SetStretchRenderer();
                    break;
                case "RGB组合":
                    this.rasterRGB1.SetRGBRenderer();
                    break;
            }
            if (this.ApplyChangedEvent != null)
            {
                this.ApplyChangedEvent(this, new EventArgs());
            }
            this.Close();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
